/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var map;
var geocoder;
function addMap(){
	//alert("smth");
	$( "#map_dialog" ).dialog( "open" );
        loadScript();
}
function initialize() {
	var width=parseInt(document.getElementById("gmap_width").value);
	var height=parseInt(document.getElementById("gmap_height").value);
	var zoom=parseInt(document.getElementById("gmap_zoom").value);
	var overview=document.getElementById("gmap_overview").checked;
	var scale=document.getElementById("gmap_scale").checked;
	var marker=document.getElementById("gmap_marker").checked;
	var lng=parseFloat(document.getElementById("gmap_lng").value);
	var lat=parseFloat(document.getElementById("gmap_lat").value);
	//alert(width+'-'+height+'-'+zoom+'-'+overview+'-'+scale+'-'+marker+'-'+lng+'-'+lat);

	document.getElementById("mapDiv").style.width=width;
	document.getElementById("mapDiv").style.height=height;
	var myLatlng = new google.maps.LatLng(lat, lng);
	//var overviewoptions= new google.maps.OverviewMapControlOptions({opened:true});
	var overviewoptions= {opened:true};
	var myOptions = {
		zoom: zoom,
		center: myLatlng,
		overviewMapControl:overview,
		overviewMapControlOptions:overviewoptions,
		scaleControl:scale,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	if(map='undefined'){
		map = new google.maps.Map(document.getElementById("mapDiv"), myOptions);
		geocoder = new google.maps.Geocoder();
		google.maps.event.addListener(map, 'mousedown', function(event) {
			var myLatLng = event.latLng;
			document.getElementById("gmap_lat").value = myLatLng.lat();
			document.getElementById("gmap_lng").value = myLatLng.lng();
		});
		if(marker){
			var mymarker = new google.maps.Marker({
	          position: map.getCenter(),
	          map: map
	        });
		}
	}
	else{
		map.setOptions(myOptions);
		if(marker){
			var mymarker = new google.maps.Marker({position: map.getCenter(),	map: map});
		}
	}


}

function loadScript() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
  document.body.appendChild(script);
}

function getAddress(){
	var address= document.getElementById("gmap_address").value;
    geocoder.geocode( {'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        document.getElementById("gmap_lng").value=results[0].geometry.location.lng();
        document.getElementById("gmap_lat").value=results[0].geometry.location.lat();
      } else {
        alert("Error, Try Again Later ");
      }
    });
}
function addmaptocontex(){
	var width=parseInt(document.getElementById("gmap_width").value);
	var height=parseInt(document.getElementById("gmap_height").value);
	var zoom=parseInt(document.getElementById("gmap_zoom").value);
	var overview=document.getElementById("gmap_overview").checked;
	var scale=document.getElementById("gmap_scale").checked;
	var marker=document.getElementById("gmap_marker").checked;
	var lng=parseFloat(document.getElementById("gmap_lng").value);
	var lat=parseFloat(document.getElementById("gmap_lat").value);


	var myframe=document.createElement("iframe");
	//myframe.frameborder=0;
	myframe.style.width=width+"px";
	myframe.style.height=height+"px";

	var myhtml=document.createElement("html");
	var mapdiv=document.createElement("div");
	mapdiv.id="mapdiv";
	mapdiv.style.width=width+"px";
	mapdiv.style.height=height+"px";
	myhtml.innerHTML=mapdiv.outerHTML;
	myframe.appendChild(myhtml);
        //document.getElementById(elementRef).appendChild(myframe);
        removeEditor();
        document.getElementById(elementRef).appendChild(myframe);
        enable_edit();
        createEditor(elementRef);
	//editor.insertHtml(myframe);
        //alert(myframe.innerHTML);
	var initialize=document.createElement("script");
	initialize.type = "text/javascript";
	initialize.innerText="function initialize(){ var map = new google.maps.Map(document.getElementById('mapdiv'),"+
            "{zoom:"+ zoom+",center: new google.maps.LatLng("+lat+","+ lng+"),overviewMapControl:"+overview +",overviewMapControlOptions:"+
            "{opened:true},scaleControl:"+scale +",mapTypeId: google.maps.MapTypeId.ROADMAP}"+
            ");"+
	"if("+marker+"){ marker = new google.maps.Marker({position: map.getCenter(),	map: map});}"
	+"}";

	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
        myhtml.innerHTML +=script.outerHTML;
        myhtml.innerHTML +=initialize.outerHTML;
        //alert(myframe.innerHTML);
	//myframe.appendChild(script);
	//myframe.appendChild(initialize);
        myframe.scrolling="no";
        myframe.frameborder="0";
        myframe.attributes.frameborder="0";
        var doc =myframe.document;
        if(myframe.contentDocument)
            doc = myframe.contentDocument; // For NS6
        else if(myframe.contentWindow)
            doc = myframe.contentWindow.document; // For IE5.5 and IE6
        // Put the content in the iframe
        doc.open();
        doc.writeln(myhtml.outerHTML);
        doc.close();
	//document.body.appendChild(myframe);
	$( "#map_dialog" ).dialog( "close" );
}
//window.onload = loadScript;


